package mall.service.impl;

import mall.bean.Cart;
import mall.bean.CartPK;
import mall.dao.BaseDao;
import mall.dao.CartDao;
import mall.dao.impl.CartDaoImpl;
import mall.service.CartService;

public class CartServiceImpl extends BaseServiceImpl<Cart, CartPK> implements CartService {

	private CartDao cartDao = new CartDaoImpl();

	@Override
	public BaseDao<Cart, CartPK> getDao() {
		return this.cartDao;
	}
	
	@Override
	public boolean save(Cart obj) {
		long uid = obj.getUid();
		long pid = obj.getPid();
		int q = obj.getQuantity();
		CartPK pk = new CartPK(uid, pid);
		// 先查cart表
		Cart record = this.cartDao.findById(pk);
		// 如果已经有这个记录了, 说明该用户已经添加过了这个商口到购物车中
		if(record != null) {
			// 加数量
			record.setQuantity(record.getQuantity() + q);
			this.cartDao.update(record);
		} else {
			// 否则就正常插入一条cart表的记录
			this.cartDao.save(obj);
		}
		return true;
	}

}
